package hanelsoft.vn.dictionaylearn.model.db;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.util.ArrayList;

import android.util.Log;

public class DatabaseFileList {
	static final private String FILELIST_TAG = "[DatabaseFileList]";
	public ArrayList<DatabaseFile> items;
	public DatabaseFileList(String dbPath, String dbExtension)
	{
		items = new ArrayList<DatabaseFile>();
		getDatabaseFileList(dbPath,dbExtension);
	}

	private void getDatabaseFileList(String dbPath, String dbExtension)
	{
		items.clear();
		File dataDirectory = new File(dbPath);
		if (!dataDirectory.exists()) //Data directory doesn't exist, create it
		{
			if (!dataDirectory.mkdirs())
			{
				Log.i(FILELIST_TAG, "Can not create directory on sdcard");
			}
			else
			{
				Log.i(FILELIST_TAG, "Data directory was created on sdcard");
			}
		}
        FileFilter ffDir = new FileFilter()
        {
        	public boolean accept(File f)
        	{
        		return f.isDirectory();
        	}
        };

        File[] lstDirectory = dataDirectory.listFiles(ffDir);
        if (lstDirectory != null && lstDirectory.length > 0)
        {
	        for (File currentDirectory : lstDirectory)
	        {
	            DatabaseFile db = new DatabaseFile();
	        	String path = currentDirectory.getAbsolutePath() + "/" + currentDirectory.getName();
          		db.fileName = currentDirectory.getName();
          		db.path = currentDirectory.getPath();
	          	File ifoFile = new File(path + ".ifo");
          		if (ifoFile.exists())
	          	{
	          		String data;
	          		String arrData[] = null;
	          		try
	          		{
			          	@SuppressWarnings("resource")
						BufferedReader brIfoFile = new BufferedReader(new FileReader(ifoFile));
			          	while (brIfoFile.ready())
			          	{
				          	data = brIfoFile.readLine();
				          	arrData = data.split("=");
				          	arrData[0] = arrData[0].trim();
				          	if (arrData[0].equals("name"))
				          	{
				          		db.dictionaryName = arrData[1].trim();
				          	}
				          	else if (arrData[0].equals("from"))
				          	{
				          		db.sourceLanguage = arrData[1].trim();
				          	}
				          	else if (arrData[0].equals("to"))
				          	{
				          		db.destinationLanguage= arrData[1].trim();
				          	}
				          	else if (arrData[0].equals("style"))
				          	{
				          		db.style= arrData[1].trim();
				          	}
			          	}
	          		}
	          		catch (Exception ex){
	          			db.dictionaryName = db.fileName;
	          			Log.e(FILELIST_TAG, "Can not read ifo file!");
	          		}
	          	}
          		else
          		{
          			db.dictionaryName = db.fileName;
          			Log.i(FILELIST_TAG, "No ifo file found, set dictionary name to file name");
          		}
          		items.add(db);
	        }
	        Log.i(FILELIST_TAG,"Found " + items.size() + " dictionaries");
        }
        else
        {
        	Log.i(FILELIST_TAG,"Do not find any valid dictionary");
        }
	}
}
